package com.ibm.eg.cvmanager.business.stub;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.RollbackException;

import com.ibm.eg.cvmanager.business.interfaces.InterviewBusiness;
import com.ibm.eg.cvmanager.dao.impl.ApplicantDao;
import com.ibm.eg.cvmanager.dao.impl.InterviewDao;
import com.ibm.eg.cvmanager.dto.Applicant;
import com.ibm.eg.cvmanager.dto.INTERVIEW_STATUS;
import com.ibm.eg.cvmanager.dto.Interview;
import com.ibm.eg.cvmanager.dto.Interviewer;
import com.ibm.eg.cvmanager.dto.InterviewerId;


public class InterviewBusinessImpl implements InterviewBusiness
{
	List<Interview> l = new ArrayList<Interview>();
	
	static int applicantId = 1;
	
	public InterviewBusinessImpl() {
		
		Interview inter1 = new Interview();
		Interview inter2 = new Interview();
		Interview inter3 = new Interview();
		Interview inter4 = new Interview();

		Set<Interviewer> set = new HashSet<Interviewer>();
		
		Interviewer interviewer1 = new Interviewer();
		
		InterviewerId id1 = new InterviewerId();
		
		id1.setInterviewId(2);
		id1.setName("Abdel Rehim");
		
		interviewer1.setId(id1);
		interviewer1.setInterview(inter4);
		
		Interviewer interviewer2 = new Interviewer();
		
		InterviewerId id2 = new InterviewerId();
		
	    id2.setInterviewId(1);
	    id2.setName("Abdel Rehim");
	    
	    interviewer2.setId(id2);
	    interviewer2.setInterview(inter3);
		
		set.add(interviewer1);
		set.add(interviewer2);
		
		inter1.setId(1100);
		inter1.setInterviewdate(new Date());
		inter1.setStatus(0);
		
		Applicant app=new Applicant();
		app.setName("abbas");
		app.setId(1);
		
		inter1.setComment("good1");
		inter1.setInterviewdate(new Date());
		inter1.setStatus(1);
		inter1.setApplicant(app);
		inter1.setInterviewers(set);
		
		inter2.setId(1102);
		inter2.setInterviewdate(new Date());
		inter2.setStatus(1);
		
		Applicant app1=new Applicant();
		app1.setName("nona");
		app1.setId(2);
		
		inter2.setComment("good2");
		inter2.setInterviewdate(new Date());
		inter2.setStatus(2);
		inter2.setApplicant(app1);
		inter2.setInterviewers(set);
		
		
		inter3.setId(1103);
		inter3.setInterviewdate(new Date());
		inter3.setStatus(1);
		
		Applicant app2=new Applicant();
		app2.setName("nona");
		app2.setId(3);
		
		inter3.setComment("good3");
		inter3.setInterviewdate(new Date());
		inter3.setStatus(0);
		inter3.setApplicant(app2);
		inter3.setInterviewers(set);
		
		inter4.setId(1200);
		inter4.setInterviewdate(new Date());
		inter4.setStatus(1);
		
		Applicant app4=new Applicant();
		app4.setName("nona");
		app4.setId(4);
		
		inter4.setComment("good4");
		inter4.setInterviewdate(new Date());
		inter4.setStatus(1);
		inter4.setApplicant(app4);
		inter4.setInterviewers(set);
		
		l.add(inter1);
		l.add(inter2);
		l.add(inter3);
		l.add(inter4);
	}
	
	public void addInterview(Interview interview)
	{
		interview.getApplicant().setId(applicantId++);
		l.add(interview);
	}
	public void updateInterview(Interview interview)
	{
		for(int i = 0;i < l.size();i++) {
			if(l.get(i).getId() == interview.getId()) {
				l.set(i, interview);
				break;
			}
		}
	}
	public void deleteInterview(int id)
	{
		for(int i = 0;i < l.size();i++) {
			if(l.get(i).getId() == id) {
				l.remove(i);
				break;
			}
		}
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ArrayList<Interview> getAll()
	{		
		return (ArrayList<Interview>) l;
	}

	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate, INTERVIEW_STATUS status)
	{
		ArrayList<Interview> f = new ArrayList<Interview>();
		
		for(Interview interview : l) {
			if(interview.getStatus() == status.ordinal()) {
				f.add(interview);
			}
		}
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return f;
	}
	
	public ArrayList<Interview> filterInterviews(Date startDate, Date endDate)
	{
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return getAll();
	}
	
	public void markAsFailed(int interviewId)
	{
		for(int i = 0;i < l.size();i++) {
			if(l.get(i).getId() == interviewId) {
				l.get(i).setStatus(INTERVIEW_STATUS.FAILED);
				break;
			}
		}		
	}
	public void markAsSucceeded(int interviewId)
	{
		for(int i = 0;i < l.size();i++) {
			if(l.get(i).getId() == interviewId) {
				l.get(i).setStatus(INTERVIEW_STATUS.SUCCEEDED);
				break;
			}
		}				
	}

	public Interview getInterview(int id) {
	
		for(int i = 0;i < l.size();i++) {
			if(l.get(i).getId() == id) {
			
				return l.get(i);
				
			}
		}
	
	/*	Applicant app=new Applicant();
		app.setName("ahmed");
		Interview inter1 = new Interview();
		inter1.setComment("good person");
		
		Date date=new Date(1900,3,1);
		inter1.setInterviewdate(date);
		
		inter1.setStatus(0);
		inter1.setApplicant(app);
		Set<Interviewer> set = new HashSet<Interviewer>();
		Interviewer in=new Interviewer();
		InterviewerId id1 = new InterviewerId();
		in.setId(id1);
	    in.getId().setName("Akram");
	    set.add(in);
		
		inter1.setInterviewers(set);*/
		
		
		return null;
		
	}
	
}
